<template>
	<view>
		<view class="head-image"><view class="image" @click="onBack"></view></view>

		<view class="sms_wrapper">
			<view class="sms-tel-box">
				<view class="lable-sms"><text>输入短信验证码</text></view>
				<view class="sms-tel">
					<text>验证码已发送至</text>
					<text>(+86){{phone}}</text>
				</view>
			</view>
			<view class="wakary-input-box"><wakary-input type="bottom" @finish="onSmsFinish"></wakary-input></view>
			<view class="reset-box" @click="onSendSms">
				<text v-if="smsBtnDisabled">重新获取({{ wait }})</text>
				<text v-else>获取验证码</text>
				<!-- <button class="smsBtn" type="default" @click="onSendSms" :disabled="smsBtnDisabled">{{ wait == 6 ? '获取验证码' : '重新获取(' + wait + ')' }}</button> -->
			</view>
		</view>
	</view>
</template>

<script>
import wakaryInput from '@/components/wakary-input/wakary-input.vue';
export default {
	components: {
		wakaryInput
	},
	data() {
		return {
			wait: 6,
			smsBtnDisabled: false,
			phone: ''
		};
	},
	onLoad(option) {
		console.log(option);
		this.phone = option.phone;
	},
	onReady() {
		// // #ifdef APP-PLUS

		// var backgroundColor = '';
		// uni.request({
		// 	url: 'http://hzcfcc.xicp.net:81/szcfapp/json/update.json',
		// 	success: sereverdata => {
		// 		console.log(sereverdata);
		// 		console.log('plus.runtime.appid：' + plus.runtime.appid);
		// 		plus.runtime.getProperty(plus.runtime.appid, function(info) {
		// 			console.log('info' + JSON.stringify(info));
		// 			var serverversion = parseInt(sereverdata.versionCode);
		// 			var currentversion = parseInt(info.versionCode);
		// 			console.log('服务端版本号：' + serverversion + '\n客户端版本号：' + currentversion);
		// 			if (serverversion > currentversion) {
		// 				backgroundColor = 'red';
		// 				//mui.confir
		// 				UPDATE.downloadWgt(sereverdata.updateFile);
		// 			}
		// 		});
		// 	},
		// 	fail: response => {
		// 		console.log(JSON.stringify(response));
		// 	}
		// });
		// //检查更新
		// var UPDATE = {
		// 	downloadWgt: function(url) {
		// 		var _this = this;
		// 		plus.nativeUI.showWaiting('正在下载更新文件...');
		// 		var task = plus.downloader.createDownload(
		// 			url,
		// 			{
		// 				filename: '_doc/update/'
		// 			},
		// 			function(d, status) {
		// 				if (status === 200) {
		// 					//alert('下载成功'+d.filename);
		// 					//关闭等待框
		// 					plus.nativeUI.closeWaiting();
		// 					//开始安装更新包
		// 					_this.installWgt(d.filename);
		// 				} else {
		// 					alert('下载更新文件失败！');
		// 				}
		// 			}
		// 		);
		// 		task.start();
		// 	},
		// 	//安装更新包
		// 	installWgt: function(filepath) {
		// 		plus.nativeUI.showWaiting('正在安装更新文件...');
		// 		//正在安装
		// 		plus.runtime.install(
		// 			filepath,
		// 			{},
		// 			function() {
		// 				plus.nativeUI.closeWaiting();
		// 				plus.nativeUI.alert('应用资源更新完成！', function() {
		// 					//安装成功重启应用。
		// 					plus.runtime.restart();
		// 					plus.nativeUI.closeWaiting();
		// 				});
		// 			},
		// 			function(e) {
		// 				plus.nativeUI.closeWaiting();
		// 				plus.nativeUI.alert('安装资源文件失败[' + e.code + ']：' + e.message);
		// 			}
		// 		);
		// 	}
		// }; //检查更新-
		// // #endif
	},
	methods: {
		onSendSms() {
			//发送验证码http://192.168.1.110:9200/agency-center/v1/agency/loginSendSms
			//http://8.129.162.11:9200/agency-center/v1/agency/sendSms
			var that = this;
			uni.request({
				url: 'http://8.129.162.11:9200/agency-center/v1/agency/loginSendSms',
				data: { mobile: that.phone },
				method: 'post',
				success: response => {
					console.log(response);
					
					uni.showToast({ title: response.data.message, icon: 'none' });
				},
				fail: response => {
					console.log(JSON.stringify(response));
					uni.showToast({ title: '请稍后重试。', icon: 'none' });
						
				} 
			});
			var onSendSms = function(that) {
				if (that.wait == 0) {
					that.smsBtnDisabled = false;
					that.wait = 6;
				} else {
					that.smsBtnDisabled = true;
					that.wait--;
					setTimeout(function() {
						onSendSms(that);
					}, 1000);
				}
			};
			onSendSms(that);
		},
		onSmsFinish(sms) {
			//验证码输入完毕，跳转登陆
			var that = this;
			//console.log(that.phone, '的验证码是' + sms);
			var data = {
				deviceId: that.phone,
				validCode: sms
			};
			uni.request({
				url: 'http://8.129.162.11:9200/agency-center/v1/agency/securityCodeLogin',
				header: data,
				method: 'post',
				success: response => {
					console.log('注册登陆',response);
					if (response.statusCode == 200) {
						uni.navigateTo({
							url: '../../app/index'
						});
					}else{
						uni.showToast({ title: response.data.message, icon: 'none' });
					}
				},
				fail: response => {
					console.log(JSON.stringify(response));
				}
			});
		},
		onBack() {
			//右上角，点叉返回上一页
			uni.navigateBack({});
		}
	}
};
</script>

<style>
.head-image {
	padding: 0px 38upx 0upx 58upx;
	box-sizing: border-box;
	width: 100%;
	margin-top: 200upx;
	height: 32upx;
	display: flex;
	justify-content: flex-end; /*富裕空间的位置*/
}
.head-image .image {
	width: 32upx;
	height: 32upx;
	background-image: url(../../images/del_max.png);
	background-size: 32upx 32upx;
}
.sms_wrapper {
	padding: 0 58upx 0 58upx;
}
.sms-tel-box {
	padding-top: 114upx;
}

.sms-tel-box .lable-sms {
	font-size: 48upx;
	font-weight: 500;
	color: rgba(77, 76, 84, 1);
}
.sms-tel-box .sms-tel text:nth-last-of-type(0) {
	margin-top: 22upx;
	height: 28upx;
	font-size: 28upx;
	font-weight: 400;
	color: #757980;
}

.sms-tel-box .sms-tel text:nth-last-of-type(1) {
	margin-top: 22upx;
	height: 28upx;
	font-size: 28upx;
	font-weight: 400;
	color: #e64545;
}
.wakary-input-box {
	margin-top: 112upx;
}

/* button {
	padding-let: 10upx;
	text-align: left;
	
	font-size:28upx;
	font-family:PingFang SC;
	font-weight:400;
	color:rgba(117,121,128,1);
}
button::after {
	border: none;
}
button[type='default'] {
	color: rgba(0, 0, 0, 0.1);
	background-color: #ffffff;
}
button:not([type]), uni-button[disabled][type=default] {  
    color: rgba(0, 0, 0, 0.1);
    background-color: #ffffff;
} */
.reset-box {
	margin-top: 52upx;
}
.reset-box text:nth-child(1) {
	height: 28upx;
	font-size: 28upx;
	font-family: PingFang SC;
	font-weight: 400;
	color: rgba(117, 121, 128, 1);
}
.reset-box text:nth-child(2) {
	height: 28upx;
	font-size: 28upx;
	font-family: PingFang SC;
	font-weight: 400;
	color: rgba(68, 70, 74, 1);
}
.reset-box text {
	margin-left: 25upx;
}
</style>
